<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible"
    >
      <el-form
        :model="inputForm"
        ref="inputForm"
        v-loading="loading"
        :class="method === 'view' ? 'readonly' : ''"
        :disabled="method === 'view'"
        label-width="120px"
      >
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="标题" prop="title" :rules="[]">
              <el-input
                v-model="inputForm.title"
                placeholder="请填写标题"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="跳转链接" prop="jumpLink" :rules="[]">
              <el-input
                v-model="inputForm.jumpLink"
                placeholder="请填写跳转链接"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="轮播图类型"
              prop="rotationChartType"
              :rules="[
                {
                  required: true,
                  message: '请选择轮播图类型',
                  trigger: 'blur',
                },
              ]"
            >
              <el-select
                v-model="inputForm.rotationChartType"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in $dictUtils.getDictList('carousel_img_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="程序类型"
              prop="carouselType"
              :rules="[
                { required: true, message: '请选择程序类型', trigger: 'blur' },
              ]"
            >
              <el-select
                v-model="inputForm.carouselType"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in $dictUtils.getDictList('program_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="图片路径"
              prop="picturePath"
              :rules="[
                { required: true, message: '请填写图片路径', trigger: 'blur' },
              ]"
            >
              <el-upload
                ref="picturePath"
                v-if="visible"
                list-type="picture-card"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vcarouselchart/vCarouselChart`"
                :headers="{ token: $cookie.get('token') }"
                :on-preview="
                  (file, fileList) => {
                    showImg(file.url);
                  }
                "
                :on-success="
                  (response, file, fileList) => {
                    inputForm.picturePath = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                "
                :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.picturePath = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                "
                :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                "
                multiple
                :limit="5"
                :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 5 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                "
                :file-list="picturePathArra"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <!-- 移动端轮播尺寸 -->
              <span
                style="color: #e6a23c"
                v-if="
                  inputForm.carouselType == 1 &&
                  (inputForm.rotationChartType == 1 ||
                    inputForm.rotationChartType == 2 ||
                    inputForm.rotationChartType == 3 ||
                    inputForm.rotationChartType == 4 ||
                    inputForm.rotationChartType == 6)
                "
                >尺寸为：360*180</span
              >
              <span
                style="color: #e6a23c"
                v-if="
                  inputForm.carouselType == 1 &&
                  (inputForm.rotationChartType == 5 ||
                    inputForm.rotationChartType == 7)
                "
                >尺寸为：187*135</span
              >
              <!-- PC端尺寸大小 -->
              <span style="color: #e6a23c" v-if="inputForm.carouselType == 2"
                >尺寸为：360*350</span
              >
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="是否启用"
              prop="whetherEnable"
              :rules="[
                { required: true, message: '请选择启用状态', trigger: 'blur' },
              ]"
            >
              <el-select
                v-model="inputForm.whetherEnable"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option label="未启用" value="0"></el-option>
                <el-option label="启用" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort" :rules="[]">
              <el-input
                v-model="inputForm.sort"
                placeholder="请填写排序"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="explain" :rules="[]">
              <WangEditor ref="contentEditor" v-model="inputForm.explain" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks" :rules="[]">
              <el-input
                type="textarea"
                v-model="inputForm.remarks"
                placeholder="请填写备注信息"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button
          type="primary"
          v-if="method != 'view'"
          @click="doSubmit()"
          v-noMoreClick
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import WangEditor from "@/components/editor/WangEditor";

export default {
  data() {
    return {
      title: "",
      method: "",
      visible: false,
      loading: false,
      picturePathArra: [],
      inputForm: {
        id: "",
        title: "",
        picturePath: "",
        jumpLink: "",
        explain: "",
        carouselType: "",
        rotationChartType: "",
        whetherEnable: "",
        sort: "",
        remarks: "",
      },
    };
  },
  components: { WangEditor },
  methods: {
    showImg(url) {
      this.$hevueImgPreview(url);
    },
    init(method, id) {
      this.method = method;
      this.inputForm.id = id;
      if (method === "add") {
        this.title = `新建车主轮播图管理`;
      } else if (method === "edit") {
        this.title = "修改车主轮播图管理";
      } else if (method === "view") {
        this.title = "查看车主轮播图管理";
      }
      this.picturePathArra = [];
      this.visible = true;
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields();
        this.$refs.contentEditor.init("");
        if (method === "edit" || method === "view") {
          // 修改或者查看
          this.loading = true;
          this.$http({
            url: `/vcarouselchart/vCarouselChart/queryById?id=${this.inputForm.id}`,
            method: "get",
          }).then(({ data }) => {
            this.inputForm = this.recover(this.inputForm, data.vCarouselChart);
            this.$refs.contentEditor.init(this.inputForm.explain);
            this.inputForm.picturePath.split("|").forEach((item) => {
              if (item.trim().length > 0) {
                this.picturePathArra.push({
                  name: decodeURIComponent(
                    item.substring(item.lastIndexOf("/") + 1)
                  ),
                  url: item,
                });
              }
            });
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    doSubmit() {
      this.$refs["inputForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http({
            url: `/vcarouselchart/vCarouselChart/save`,
            method: "post",
            data: this.inputForm,
          }).then(({ data }) => {
            this.loading = false;
            if (data && data.success) {
              this.visible = false;
              this.$message.success(data.msg);
              this.$emit("refreshDataList");
            }
          });
        }
      });
    },
  },
};
</script>
